#include "gtrans.h"
#include "ui_gtrans.h"

GTrans::GTrans(QString connexionName,int id ,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::GTrans)
{
    ui->setupUi(this);

    // On initialise db et idConnexion
    db = QSqlDatabase::database(connexionName);
    idConnexion = id;
    QSettings s;

    // ------------------------  Initialisation de l'onglet transactions ----------------------------------------
    //On initialise la table des transactions

    modelTransactions = new QSqlTableModel(this,db);
    modelTransactions->setTable("v_transactions");
    modelTransactions->setSort(1,Qt::DescendingOrder);
    modelTransactions->select();
    modelTransactions->setHeaderData(0,Qt::Horizontal,"Id");
    modelTransactions->setHeaderData(1,Qt::Horizontal,"Date");
    modelTransactions->setHeaderData(2,Qt::Horizontal,"Id Déb.");
    modelTransactions->setHeaderData(3,Qt::Horizontal,"Pseudo Débiteur");
    modelTransactions->setHeaderData(4,Qt::Horizontal,"Fam's");
    modelTransactions->setHeaderData(5,Qt::Horizontal,"Prom's");
    modelTransactions->setHeaderData(6,Qt::Horizontal,"Id Cré.");
    modelTransactions->setHeaderData(7,Qt::Horizontal,"Pseudo Créditeur");
    modelTransactions->setHeaderData(8,Qt::Horizontal,"Fam's");
    modelTransactions->setHeaderData(9,Qt::Horizontal,"Prom's");
    modelTransactions->setHeaderData(10,Qt::Horizontal,"Montant en €");
    modelTransactions->setHeaderData(11,Qt::Horizontal,"Intitulé");
    ui->tableTransDetail->setModel(modelTransactions);
    ui->tableTransDetail->hideColumn(0);
    ui->tableTransDetail->hideColumn(2);
    ui->tableTransDetail->hideColumn(4);
    ui->tableTransDetail->hideColumn(6);
    ui->tableTransDetail->hideColumn(8);

    //Avec un delegate pour le champs montant
    MoneyDelegate *mdTrans = new MoneyDelegate(ui->tableTransDetail);
    ui->tableTransDetail->setItemDelegateForColumn(10,mdTrans);

    ui->tableTransDetail->resizeColumnsToContents();

    //On initialise les filtres
    ui->dateTransDetailsDate->setDate(QDate::currentDate());
    connect(ui->checkTransDetailPlus,SIGNAL(toggled(bool)),ui->dateTransDetailsDate,SLOT(setEnabled(bool)));
    connect(ui->tableTransDetail->selectionModel(),SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this,SLOT(sommeSelectionnee()));

    // ------------------------  Initialisation de l'onglet remise ----------------------------------------
    //On initialise l'id selectionné
    idRemiseSelected = 0;
    nomtmp = "";
    prenomtmps = "";
    ui->comboRemiseAjoutBanque->addItems(s.value("trans/banques").toStringList());


    // ------------------------  Initialisation de l'onglet param ----------------------------------------

    QStringList banques;
    banques << "BNP Paribas" << "Société Générale" << "Crédit Mutuel" << "LCL" << "CIC" << "HSBC" << "Crédit Agricole" << "La Banque Postale" << "Banque Populaire" << "Caisse d'Epargne" ;
    if(s.value("trans/banques").toStringList().isEmpty()){
        s.setValue("trans/banques",banques);
    }

    ui->lineParamConnexionHost->setText(s.value("connexion/host","").toString());
    ui->listParamBanque->addItems(s.value("trans/banques",banques).toStringList());

    if(s.value("trans/compte",0).toInt() != 0){
        QSqlQuery q(db);
        q.prepare("SELECT fams, proms FROM v_users WHERE id = :i");
        q.bindValue(":i",s.value("trans/compte",0).toInt());
        q.exec();
        q.first();
        ui->spinParamCompteFams->setValue(q.value(0).toInt());
        ui->comboParamCompteProms->setCurrentText(q.value(1).toString());
    }

    // ------------------------  Initialisation de l'onglet Agios ----------------------------------------
    // On initialise la table des agios
    modelAgiosComptes = new QSqlQueryModel();
    ui->tableAgiosApercu->setModel(modelAgiosComptes);

    //Charger le delegate
    MoneyDelegate *m = new MoneyDelegate(ui->tableAgiosApercu);
    ui->tableAgiosApercu->setItemDelegateForColumn(5,m);
    ui->tableAgiosApercu->setItemDelegateForColumn(6,m);
    ui->tableAgiosApercu->setItemDelegateForColumn(7,m);

    // On initialise les proms
    modelAgiosProms = new QSqlQueryModel();
    on_buttonAgiosPromsRaffraichir_clicked();
    ui->listAgiosProms->setModel(modelAgiosProms);
    ui->listAgiosProms->setModelColumn(0);

    // On vide les labels
    ui->labelAgiosNbComptes->setText("0");
    ui->labelAgiosNbExemptes->setText("0");

    // On initialise les variables
    listExempts.clear();
    requeteBaseAgios = "SELECT id, bucque, fams, proms, actif, solde, TRUNCATE(solde * :a / 100 , 2), TRUNCATE(solde * (100 + :a) /100 , 2) FROM v_users ";
    nbComptesAgios = 0;
    isReloading = false;

    // On connecte les signaux
        // Pour les mises à jours des filtres permanents on simule un clic sur aperçu qui provoque la RaZ des filtres Temporaires et la MaJ de la table et des compteurs
    connect(ui->spinAgiosCommandesAgios,SIGNAL(editingFinished()),this,SLOT(on_buttonAgiosApercu_clicked()));
    connect(ui->spinAgiosCommandesSeuil,SIGNAL(editingFinished()),this,SLOT(on_buttonAgiosApercu_clicked()));
    connect(ui->spinAgiosRechercheFams,SIGNAL(editingFinished()),this,SLOT(raffraichirApercuAgios()));
    connect(ui->lineAgiosRechercheBucque,SIGNAL(editingFinished()),this,SLOT(raffraichirApercuAgios()));
    connect(ui->checkAgiosPromsInactifs,SIGNAL(toggled(bool)),this,SLOT(on_buttonAgiosApercu_clicked()));
    connect(ui->listAgiosProms,SIGNAL(clicked(QModelIndex)),this,SLOT(on_buttonAgiosApercu_clicked()));
    connect(ui->tableAgiosApercu->selectionModel(),SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this,SLOT(modifSelection(QItemSelection,QItemSelection)));

}

GTrans::~GTrans()
{
    delete ui;

    //Tab transactions
    delete modelTransactions;

    //Tab remise
    delete modelRemise;
    delete modelRemiseDetail;

    //Tab Agios
    delete modelAgiosComptes;
    delete modelAgiosProms;

}

